package com.wl.application.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/15
 * @description: 全局异常解析器
 */
//标志了该类是全局异常处理器
@RestControllerAdvice
@Slf4j
public class MyExceptionResolver {

    /**
     * 系统异常的处理

     */
    @ExceptionHandler(Exception.class)
    public Result AllException(Exception e) {
        //创建日志对象
        Logger logger = LoggerFactory.getLogger(MyExceptionResolver.class);

        //记录系统出错信息
        logger.error("系统异常!", e);
        return Result.error("系统繁忙,请稍后再试!");
    }


    /**
     * 业务异常的处理
     */
    @ExceptionHandler(BusinessException.class)
    public Result BusinessException(BusinessException e) {
        //创建日志对象
        Logger logger = LoggerFactory.getLogger(MyExceptionResolver.class);
        //记录系统出错信息
        logger.error("业务异常!", e);
        return Result.error("出现业务错误了!");
    }

}
